package com.nineclock.attendance.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

//使用Feign的拦截器来解决认证截断， 拦截所有的feign的远程调用，
// 在进行远程调用时， 拦截住请求， 并让请求头Authorization 继续往下传递

@Component
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        RequestAttributes requestAttribute= RequestContextHolder.getRequestAttributes();
        if (requestAttribute!=null){
            HttpServletRequest request = ((ServletRequestAttributes) requestAttribute).getRequest();
            String authorization = request.getHeader("Authorization");
            requestTemplate.header("Authorization",authorization);
        }

    }
}
